/**
 * Created by yuanchao on 2017/9/17.
 * 通用的store存储和获取的方法.
 * 存储后: Storage {_seller_: "{"12345":{"favorite":true}}",length:1}
 */
export function saveToLocal(id, key, value) {
  let seller = window.localStorage._seller_;
  if (!seller) {
    seller = {};
    seller[id] = {};
  } else {
    seller = JSON.parse(seller);
    if (!seller[id]) {
      seller[id] = {};
    }
  }
  // 通过上面的分支语句,保证了seller存在,seller[id]也存在了.
  seller[id][key] = value;
  window.localStorage._seller_ = JSON.stringify(seller);
};

export function loadFromLocal(id, key, def) {
  let seller = window.localStorage._seller_;
  if (!seller) {
    return def;
  }
    seller = JSON.parse(seller)[id];
    if (!seller) {
      return def;
    }
    let ret = seller[key];
    return ret || def;
};
